project(
  'minpack',
  'fortran',
  version: '2.0.0',
  meson_version: '>=0.55',
  default_options: [
    'default_library=both',
    'buildtype=debugoptimized',
    'fortran_std=f2008',
  ],
)
if get_option('api')
  has_cc = add_languages('c', native: false)
elif get_option('python')
  error('Cannot compile Python API, Fortran compiler does not support nested functions')
endif

srcs = files(
  'src/minpack.F90',
)
if get_option('api')
  srcs += files('src/minpack_capi.F90')
endif

f_compiler = meson.get_compiler('fortran')
fargs = []

# GCC Fortran 15.0 BUG
if (f_compiler.version().version_compare('>= 15.0') and
f_compiler.version().version_compare('< 16.0'))
    fargs += '-Wno-external-argument-mismatch'
    message('Applying GCC Fortran 15.0 bug workaround')
endif

minpack_lib = library(
  meson.project_name(),
  sources: srcs,
  install: true,
  fortran_args: fargs,
)

minpack_inc = minpack_lib.private_dir_include()
minpack_dep = declare_dependency(
  link_with: minpack_lib,
  include_directories: [minpack_inc, include_directories('include')],
  variables: {'includedir': meson.current_source_dir() / 'include'},
)

minpack_lic = files(
  'LICENSE.txt',
)

minpack_header = files(
  'include/minpack.h',
)

install_data(
  minpack_lic,
  install_dir: get_option('datadir')/'licenses'/meson.project_name()
)

if get_option('api')
  install_headers(
    minpack_header,
  )
endif

module_id = meson.project_name()
if host_machine.system() == 'windows'
    symbols_file = 'lib'+module_id+'-'+meson.project_version().split('.')[0]+'.dll.symbols'
    obj_suffix = '.obj'
else
    symbols_file = 'lib'+module_id+'.so.'+meson.project_version()+'.symbols'
    obj_suffix = '.o'
endif
install_subdir(minpack_lib.full_path()+'.p',
    install_dir: 'include'/module_id,
    strip_directory: true,
    exclude_files: [
        'depscan.dd',
        module_id+'-deps.json',
        symbols_file,
        module_id+'.dat',
        'src_minpack.F90'+obj_suffix,
        'src_minpack_module.F90'+obj_suffix,
    ]
)

pkg = import('pkgconfig')
pkg.generate(
  minpack_lib,
  description: 'Solver for nonlinear equations and least squares problems',
  subdirs: [module_id],
)

# add examples
subdir('examples')

# add the testsuite
subdir('test')

if get_option('python')
  subdir('python/minpack')
endif
